<?php
class Control_ServerList_configure {
 	function configure() {
 		
 		$action = MooForm::request('action');
 		
 		if($action == 1) {
 			$nowGame = MooForm::request('game');
 			// 中文转换英文 game
 			$game = MooObj::get('Game')->transportGame($nowGame);
 			$platform = MooForm::request('platform');
 			// 设置加载platform信息
 			MooObj::get('Control_Data')->setPlatformData($game);
 			
 			$serverlistDao =  MooDao::get('Serverlist');
 			
 			if($platform) {
 				$sql = 'select * from @TABLE where s_game=:game and s_platform=:platform';
 				$where = array(
					'game' => $game,			
					'platform' => $platform,			
 				);
 			} else {
 				$sql = 'select * from @TABLE where s_game=:game';
 				$where = array(
 					'game' => $game,
 				);
 			}
 			$serverlist = $serverlistDao->getAll($sql, $where);
			
 			$formatPath = ROOT_PATH . "/servistLists";
 			if(!MooFile::isExists($formatPath)) {
 				MooFile::mkDir($formatPath);
 			}
 			
 			$urlHead = MooConfig::get('main.downUrlHead');
 			// 每次配置以数据库为准 存储为文件
 			if($serverlist) {
 				foreach ($serverlist as $key => $val) {
 					$gameName 			= $val['s_game'];
 					$platfromName 		= $val['s_platform'];
 					$fileName	  		= $val['s_filename'];
 					$serverlistCfg	    = $val['s_serverlist'];
 					
 					// 格式化服务器列表
 					$serverlistCfg = $this->OBJ->formatServerList($serverlistCfg);
 					
 					$formatFile = ROOT_PATH . "/servistLists/{$gameName}_{$platfromName}_$fileName";
 					$serverlist[$key]['reViewUrl'] = $urlHead . "/servistLists/{$gameName}_{$platfromName}_$fileName";
 					// 每次保存 覆盖重写该文件
 					MooFile::write($formatFile, $serverlistCfg);
 				}
 			}
 			
 			MooView::set('serverlist', $serverlist);
 			MooView::set('nowGame', $nowGame);
 			MooView::set('platform', $platform);
 				
 			MooView::render("serverListConfigure");
 				
 		} else {
 			MooView::render("serverListConfigure");
 		}
 		
	}
	
}

